package com.cloud.open.controller;

import com.cloud.common.bean.PageInfo;
import com.cloud.common.bean.ResultInfo;
import com.cloud.open.constant.ErrorEnum;
import com.cloud.open.dto.ResourceInsertParamDTO;
import com.cloud.open.dto.ResourceQueryParamDTO;
import com.cloud.open.dto.ResourceUpdateParamDTO;
import com.cloud.open.entity.Resource;
import com.cloud.open.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * 资源接口
 */
@Slf4j
@RestController
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 查询资源
     *
     * @param paramDTO
     * @return ResultInfo
     */
    @PostMapping("/open/resource/query")
    public ResultInfo<PageInfo<Resource>> query(@Valid @RequestBody ResourceQueryParamDTO paramDTO) {
        PageInfo<Resource> pageInfo = resourceService.getPage(paramDTO);
        return ResultInfo.success().setData(pageInfo);
    }

    /**
     * 添加资源
     *
     * @param paramDTO
     * @return ResultInfo
     */
    @PostMapping("/open/resource/insert")
    public ResultInfo<String> insert(@Valid @RequestBody ResourceInsertParamDTO paramDTO) {
        if (!checkAccessUri(paramDTO.getAccessUri())) {
            return ErrorEnum.URI_INCORRECT.resultInfo();
        }
        if (resourceService.checkNameExist(paramDTO.getName(), paramDTO.getTypeId(), null)) {
            return ErrorEnum.NAME_EXIST.resultInfo();
        }

        if (resourceService.checkAccessUriExist(paramDTO.getAccessUri(), paramDTO.getTypeId(), null)) {
            return ErrorEnum.URI_EXIST.resultInfo();
        }

        Resource resource = new Resource();
        resource.setCreateTime(new Date());
        BeanUtils.copyProperties(paramDTO, resource);

        if (resourceService.save(resource)) {
            return ResultInfo.success();
        }
        return ResultInfo.failure();
    }

    /**
     * 更新资源
     *
     * @param paramDTO
     * @return ResultInfo
     */
    @PostMapping("/open/resource/update")
    public ResultInfo<String> update(@Valid @RequestBody ResourceUpdateParamDTO paramDTO) {
        if (!checkAccessUri(paramDTO.getAccessUri())) {
            return ErrorEnum.URI_INCORRECT.resultInfo();
        }
        if (resourceService.checkNameExist(paramDTO.getName(), paramDTO.getTypeId(), paramDTO.getId())) {
            return ErrorEnum.NAME_EXIST.resultInfo();
        }

        if (resourceService.checkAccessUriExist(paramDTO.getAccessUri(), paramDTO.getTypeId(), paramDTO.getId())) {
            return ErrorEnum.URI_EXIST.resultInfo();
        }

        Resource resource = new Resource();
        BeanUtils.copyProperties(paramDTO, resource);

        if (resourceService.updateById(resource)) {
            return ResultInfo.success();
        }
        return ResultInfo.failure();
    }

    /**
     * 删除资源
     *
     * @param id
     * @return ResultInfo
     */
    @PostMapping("/open/resource/delete")
    public ResultInfo<String> delete(Integer id) {
        if (id == null) {
            return ErrorEnum.ID_NOT_NULL.resultInfo();
        }
        resourceService.removeById(id);
        return ResultInfo.success();
    }

    private boolean checkAccessUri(String accessUri) {
        if (!accessUri.startsWith("/") || accessUri.contains("//")) {
            return false;
        }
        return true;
    }
}

